package com.group3.group3.menu.service.impl;

import com.group3.group3.menu.repository.MenuDao;
import com.group3.group3.menu.service.MenuService;
import com.group3.group3.mybatis.entity.PtMenuEntity;
import com.group3.group3.mybatis.entity.PtMenuI18nEntity;
import com.group3.group3.mybatis.ztree.ZtreeEntity;
import com.group3.group3.role.repository.RoleDao;
import com.group3.group3.spring.util.DataUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Resource
    private RoleDao roleDao;
    @Value("${ubound.uuid}")
    private String uboundUuid;
    @Resource
    private MessageSource messageSource;

    @Override
    public List<ZtreeEntity> getMenuTree() {

        ZtreeEntity ZtreeEntity = new ZtreeEntity();
        ZtreeEntity.setId(uboundUuid);
        Locale locale = LocaleContextHolder.getLocale();
        List<ZtreeEntity> list = roleDao.getMenuZtreeList(locale.toString());
        String unboundName = messageSource.getMessage("common.unbound.name", null, locale);
        ZtreeEntity.setName(unboundName);
        list.add(ZtreeEntity);
        Integer index = 0;
        for (ZtreeEntity item : list) {
            if (index < 10) {
                item.setOpen(true);
            } else {
                item.setOpen(false);
            }
            index++;
        }

        return list;
    }



    @Resource
    private MenuDao menuDao;

    @Override
    public PtMenuEntity getMenuInfoById(String menuId) {
        return menuDao.getMenuInfoById(menuId);

    }
    @Override
    public void modifyMenuInfoById(PtMenuEntity entity) {
        menuDao.modifyMenuInfoById(entity);
    }

    @Override
    public void addMenuInfoById(PtMenuEntity entity) {
        entity.setMenuId(DataUtil.getUUIDShort());
        entity.setParentId(uboundUuid);

        menuDao.addMenuInfoById(entity);
    }

    @Override
    public void deleteMenuInfoById(String menuId) {
        menuDao.deleteMenuInfoById(menuId);
    }


    @Value("${root.uuid}")
    private String rootUuid;
    @Override
    public List<ZtreeEntity> getMenuTreeByMenuId(String menuId) {
        List<ZtreeEntity> list = roleDao.queryMenuTree();
        String parentId = menuDao.queryMenuTreeByMenuId(menuId);
        if(parentId != null){
            int i = 0;
            for(ZtreeEntity item:list){
                if(parentId.equals(item.getId())){
                    item.setChecked(true);
                }
                if(i > 10 ){
                    item.setOpen(false);
                }
                i++;
            }
        }

        ZtreeEntity ZtreeEntity = new ZtreeEntity();
        ZtreeEntity.setId(rootUuid);
        Locale locale = LocaleContextHolder.getLocale();
        String rootName = messageSource.getMessage("common.root.node.name",null,locale);
        ZtreeEntity.setName(rootName);
        list.add(ZtreeEntity);
        return list;
    }
    @Override
    public void boundMenu(String menuId, String parentId) {
        menuDao.updateParentIdByMenuId(menuId,parentId);
    }


    @Override
    public void unBoundByMenuId(String menuId) {
        menuDao.updateParentIdByMenuId(menuId,uboundUuid);
    }

    @Override
    public List<PtMenuI18nEntity> getI18nByMenuId(String menuId) {
        return menuDao.getI18nByMenuId(menuId);
    }




    @Override
    public void saveI18nData(PtMenuI18nEntity entity) {
        menuDao.saveI18nData(entity);
    }

    @Override
    public void addI18nData(PtMenuI18nEntity entity) {
        menuDao.addI18nData(entity);
    }

    @Override
    public void deleteI18nData(PtMenuI18nEntity entity) {
        menuDao.deleteI18nData(entity);
    }

}